package com.lm.scau.dyb.db.Dalx;

import android.content.ContentValues;
import android.database.Cursor;

import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.bean.Text;
import com.lm.scau.dyb.db.DybDB;

/**
 * Created by Administrator on 2016/8/20.
 */
public class TextDalx extends BaseDalx{
    private static final String TB_NAME = "tb_text";
    private static final String TX_ID = "tx_id";
    private static final String TX_USERID = "tx_userid";
    private static final String TX_PHURL1 = "tx_phurl1";
    private static final String TX_PHURL2 = "tx_phurl2";
    private static final String TX_PHURL3 = "tx_phurl3";
    private static final String TX_LOCAL = "tx_local";
    private static final String TX_POINT = "tx_point";
    private static final String TX_TIME = "tx_time";
    private static final String TX_INFO = "tx_info";

    public TextDalx(BaseActivity activity) {
        if (db == null) {
            db = DybDB.get(activity);
        }
        if (!db.tabbleIsExist(TB_NAME)) {
            createDataFileTable();
        }
    }
    private void createDataFileTable() {
        String createTableSql = "CREATE TABLE IF NOT EXISTS " + TB_NAME + "(" + TX_ID
                + " varchar, " +  TX_USERID + " varchar, " + TX_PHURL1 + " varchar, " + TX_PHURL2 + " varchar, " +
                TX_PHURL3 + " varchar, " + TX_LOCAL + " varchar, " + TX_POINT +
                " varchar, " + TX_TIME + " varchar, " + TX_INFO + " varchar)";
        db.creatTable(createTableSql);
    }
    /**
     * 获取最新的轮播时间，用来更新最新数据
     *
     * @return
     */
    @Override
    public String getLatelyTime() {
        String sql = "select " + TX_TIME + " from " + TB_NAME +
                " order by " + TX_TIME + " desc limit ?";
        Cursor cursor = db.find(sql, new String[]{"1"});
        String time = "1970-01-01 00:00:00";

        while (cursor.moveToNext()) {
            time = cursor.getString(cursor.getColumnIndex(TX_TIME));
            break;
        }
        return time;
    }

    /**
     * 新增轮播数据
     *
     * @param text
     * @return
     */
    public boolean addMovies(Text text) {
        boolean isOk = false;
        ContentValues values = new ContentValues();
        values.put(TX_ID, text.getTextID());
        values.put(TX_USERID, text.getTextUserID());
        values.put(TX_PHURL1, text.getTextPhotoUrl1());
        values.put(TX_PHURL2, text.getTextPhotoUrl2());
        values.put(TX_PHURL3, text.getTextPhotoUrl3());
        values.put(TX_LOCAL, text.getTextLocal());
        values.put(TX_TIME, text.getTextTime());
        values.put(TX_POINT, text.getTextPoint());
        values.put(TX_INFO, text.getTextInfo());
        if (db.save(TB_NAME, values)) {
            isOk = true;
        }
        return isOk;
    }
}
